package com.liuxingzhiwu.ruijishangcheng.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuxingzhiwu.ruijishangcheng.common.R;
import com.liuxingzhiwu.ruijishangcheng.entity.AddressBook;
import com.liuxingzhiwu.ruijishangcheng.entity.Orders;
import com.liuxingzhiwu.ruijishangcheng.entity.ShoppingCart;
import com.liuxingzhiwu.ruijishangcheng.entity.User;
import com.liuxingzhiwu.ruijishangcheng.service.AddressBookService;
import com.liuxingzhiwu.ruijishangcheng.service.OrdersService;
import com.liuxingzhiwu.ruijishangcheng.service.ShoppingCartService;
import com.liuxingzhiwu.ruijishangcheng.service.UserService;
import com.liuxingzhiwu.ruijishangcheng.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 分页查询订单信息
     * @param page
     * @param pageSize
     * @param number
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String number){

        //构造分页条件
        Page<Orders> pageInfo = new Page<>(page,pageSize);

        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(number),"number",number);

        ordersService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }


    /**
     * 支付订单
     * @param orders
     * @param session
     * @return
     */
    @PostMapping("/submit")
    public R<String> submitOrder(@RequestBody Orders orders, HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        //用户id
        orders.setUserId(userId);
        //订单编号
        orders.setNumber(SnowFlake.nextId());
        //订单创建时间
        orders.setOrderTime(LocalDateTime.now());
        //订单生成时间
        orders.setCheckoutTime(LocalDateTime.now());

        //金额
        //获取用户购物车商品
        List<ShoppingCart> shoppingCarts = shoppingCartService.list();

        BigDecimal amount = new BigDecimal(0);
        for (ShoppingCart sh:shoppingCarts) {
            BigDecimal am = sh.getAmount();
            BigDecimal num = new BigDecimal(sh.getNumber());
            //乘法bignum3 = bignum1.multiply(bignum2);
            //加法bignum3 =  bignum1.add(bignum2);
            amount = amount.add(am.multiply(num));
        }
        orders.setAmount(amount);

        //用户名
        User user = userService.getById((Long)session.getAttribute("user"));
        orders.setUserName(user.getName());
        //用户电话
        orders.setPhone(user.getPhone());

        //用户地址
        QueryWrapper<AddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",(Long)session.getAttribute("user"));
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        orders.setAddress(addressBook.getDetail());

        //支付成功
        ordersService.save(orders);
        //删除购物车里面的订单
        QueryWrapper<ShoppingCart> qu = new QueryWrapper<>();
        qu.clear();
        shoppingCartService.remove(qu);

        return R.success("支付成功");
    }

     @GetMapping("/userPage")
    private R<Page<Orders>> getUserPage(int page,int pageSize){
        //分页构造器
         Page<Orders> pageInfo = new Page<>(page,pageSize);
         ordersService.page(pageInfo);
        return R.success(pageInfo);
     }

}
